 package com.yixun.qfbao.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yixun.ssm001.admin.build.engine.util.JSONUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.yixun.common.controller.BaseController;
import com.yixun.common.exception.YIXUNExceptionCode;
import com.yixun.common.exception.YIXUNUNCheckedException;
import com.yixun.qfbao.enums.FileTypeEnum;
import com.yixun.qfbao.service.CommonService;
import com.yixun.qfbao.service.impl.CommonServiceImpl;
import org.springframework.web.multipart.MultipartHttpServletRequest;

 @Controller
@RequestMapping("/api")
public class ApiController extends BaseController{
	private static Logger log = Logger.getLogger(ApiController.class);
	@Autowired
	CommonService commonService;

	/**
	 * 上传文件(apk或者UI)
	 * @param file
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadFile")
	public String uploadFile(Model model,MultipartFile file,String type,HttpServletResponse response,String sourceFileName) throws IOException{
		response.setContentType("text/html; charset=utf-8");
		JSONObject res = new JSONObject();
		JSONObject resDto = new JSONObject();
		try {
			FileTypeEnum typeEn = FileTypeEnum.getByNo(type);
			String url =  commonService.saveFile(file,typeEn,sourceFileName);
			log.info("image url = " + url);
			res.put("result","1");
			res.put("imgName",url);
		}catch (YIXUNUNCheckedException e) {
			log.error("上传失败: {}",e);
			res.put("result","0");
			res.put("errorCode",e.getCode());
			res.put("errorMsg",e.getOutMsg());
		}catch (Exception e) {
			log.error("上传失败: {}",e);
			res.put("result","0");
			res.put("errorCode",YIXUNExceptionCode.UNKNOW_EXCEPTON.getCode());
			res.put("errorMsg",YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
		}
//		response.getWriter().append(res.toJSONString());
		resDto.put("resData",res);
		resDto.put("result",res.getString("result"));
		model.addAttribute("apires",resDto);
		return "apires";
	}

	/**
	 * 上传微信配置文件
	 * @method: uploadWxFile
	 * @param: [model, file, type, response, sourceFileName]
	 * @return: java.lang.String
	 */
	 @PostMapping(value = "/UploadWxFile",produces = "application/json")
	 public String uploadWxFile(Model model,MultipartFile file,String type,HttpServletResponse response) {
		 response.setContentType("text/html; charset=utf-8");
		 JSONObject res = new JSONObject();
		 JSONObject resDto = new JSONObject();
		 try {
			 FileTypeEnum typeEn = FileTypeEnum.getByNo(type);
			 String url =  commonService.saveWxFile(file);
			 log.info("image url = " + url);
			 res.put("result","1");
			 res.put("wxConfigFile",url);
		 }catch (YIXUNUNCheckedException e) {
			 log.error("上传失败: {}",e);
			 res.put("result","0");
			 res.put("errorCode",e.getCode());
			 res.put("errorMsg",e.getOutMsg());

		 }catch (Exception e) {
			 log.error("上传失败: {}",e);
			 res.put("result","0");
			 res.put("errorCode",YIXUNExceptionCode.UNKNOW_EXCEPTON.getCode());
			 res.put("errorMsg",YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
		 }
		 resDto.put("resData",res);
		 resDto.put("result",res.getString("result"));
		 model.addAttribute("apires",resDto);
		 return "apires";
	 }

}
